package com.meihua.shellapp.base;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.aspsine.irecyclerview.OnLoadMoreListener;
import com.aspsine.irecyclerview.OnRefreshListener;
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.meihua.shellapp.R;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

/**
 * Created by ${zhangyuanchao} on 2017/9/27.
 */

public abstract class BaseRecyclerViewFragment extends BaseFragment {
    public LoadMoreFooterView mLoadMoreFooterView;
    private SliderLayout mSlider;
    private RecyclerView.Adapter mAdapter;
    public TextView bannerTv;

    /*初始化irecyclerview*/
    public void initRecyclerView(final IRecyclerView recyclerView, boolean isSowBanner) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        /*binnerview*/
        if (isSowBanner && mSlider == null) {
            LinearLayout bannerView = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.layout_banner_view, recyclerView.getHeaderContainer(), false);
            mSlider = bannerView.findViewById(R.id.binnerview);
            bannerView.findViewById(R.id.banner_tv).setVisibility(View.GONE);
            //内部图片切换的时候,使用的动画类型
//            mSlider.setPresetTransformer(SliderLayout.Transformer.RotateDown);
            //设置可以轮播容器内部点的所在位置
            mSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
            //文本描述出现的时候,指定的动画
            mSlider.setCustomAnimation(new DescriptionAnimation());
            //动画的执行时长
            mSlider.setDuration(3000);
            setBannerView(mSlider);
            recyclerView.addHeaderView(bannerView);
        }
        /*加载更多底部View*/
        mLoadMoreFooterView = (LoadMoreFooterView) recyclerView.getLoadMoreFooterView();
        if (mAdapter == null) {
            mAdapter = getAdapter();
            recyclerView.setIAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
        recyclerView.setOnRefreshListener(onRefreshListener);
        recyclerView.setOnLoadMoreListener(onLoadMoreListener);
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                getServiceData(1);
            }
        });
    }

    protected abstract RecyclerView.Adapter getAdapter();

    protected abstract void getServiceData(int mPage);


    /*设置binnerView*/
    public void setBannerView(SliderLayout slider) {

    }

    private OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            if (mLoadMoreFooterView.canLoadMore() && mAdapter.getItemCount() > 0) {
                mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
                loadMore();
            }
        }
    };

    protected abstract void loadMore();

    private OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            setBannerView(mSlider);
            mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.GONE);
            refresh();
        }
    };

    protected abstract void refresh();
}
